﻿using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Text;
using System.Threading;
using OrderCms.DataAccess;

namespace 订单分流.Api
{
	// Token: 0x02000008 RID: 8
	internal class PayClassV2
	{
		// Token: 0x06000024 RID: 36 RVA: 0x00004220 File Offset: 0x00002420
		public void StartPay()
		{
			while (Appconst.threadFlag && Appconst.SoftthreadFlag)
			{
				try
				{
					this.orderDt = new DataTable();
					string SqlExt = string.Empty;
					this.threadStopwatch1.Restart();
					StringBuilder sql = new StringBuilder();
					sql = new StringBuilder();
					sql.AppendLine("SELECT TOP " + Appconst.threadNum);
					sql.AppendLine("[ID],[User_GroupId],[OrderNumber],[RechargeNumber],[RechargeNum],[RechargeAmount],[AllAmount],[CorpId],[Order_TypeId],[ProvinceId],[CityId],[OrderStatus],[OrderTimeOutSec],[User_MemberId],[RechargeIp],[OrderGuid],[CreateTime]");
					sql.AppendLine("FROM [dbo].[Order_List] WITH(ROWLOCK, UPDLOCK, READPAST,INDEX(IX_Order_List_订单状态_分流使用))");
					sql.AppendLine(string.Concat(new object[]
					{
						"WHERE OrderStatus=0 AND User_GroupId=",
						this.User_GroupId,
						" AND LastUpTime>='",
						DateTime.Now.AddHours(-24.0),
						"'"
					}));
					this.orderDt = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
					sql = new StringBuilder();
					sql.AppendLine("SELECT TOP " + Appconst.threadNum);
					sql.AppendLine("[ID],[User_GroupId],[OrderNumber],[RechargeNumber],[RechargeNum],[RechargeAmount],[AllAmount],[CorpId],[Order_TypeId],[ProvinceId],[CityId],[OrderStatus],[OrderTimeOutSec],[User_MemberId],[RechargeIp],[OrderGuid],[CreateTime]");
					sql.AppendLine("FROM [dbo].[Order_List] WITH(ROWLOCK, UPDLOCK, READPAST,INDEX(IX_Order_List_订单状态_分流使用))");
					sql.AppendLine(string.Concat(new object[]
					{
						"WHERE OrderStatus=3 AND User_GroupId=",
						this.User_GroupId,
						" AND LastUpTime<='",
						DateTime.Now.AddSeconds(-30.0),
						"'"
					}));
					DataTable orderDt2 = DbHelper.GetDataTable(CommandType.Text, sql.ToString(), new DbParameter[0]);
					this.orderDt.Merge(orderDt2);
					this.获取订单耗时 = this.threadStopwatch1.ElapsedMilliseconds;
					Appconst.GetThreadPoolNum();
					bool flag = this.orderDt.Rows.Count == 0 && Appconst.threadFlag;
					if (flag)
					{
						Thread.Sleep(1000);
					}
					else
					{
						bool flag2 = this.orderDt.Rows.Count > 0;
						if (flag2)
						{
							this.threadStopwatch1.Restart();
							Thread[] Threads = new Thread[this.orderDt.Rows.Count];
							for (int i = 0; i < Threads.Length; i++)
							{
								Threads[i] = new Thread(new ThreadStart(new NotifyClass
								{
									orderDt = this.orderDt,
									orderListDr = this.orderDt.Rows[i],
									OrderNumberStart = this.OrderNumberStart
								}.StartOrder));
								Threads[i].IsBackground = true;
								Threads[i].Start();
							}
							for (int j = 0; j < Threads.Length; j++)
							{
								Threads[j].Join();
							}
							this.处理订单耗时 = this.threadStopwatch1.ElapsedMilliseconds;
							Appconst.ShowLog(string.Concat(new object[]
							{
								"线程名:",
								this.OrderNumberStart,
								" 获取耗时:",
								this.获取订单耗时,
								" 处理耗时:",
								this.处理订单耗时
							}));
						}
					}
				}
				catch (Exception ex)
				{
					PayClassV2.log.Error("线程名:" + this.OrderNumberStart + " 获取分流订单失败", ex);
					Appconst.ShowLog("线程名:" + this.OrderNumberStart + " 获取分流订单失败" + ex.Message);
					Thread.Sleep(5000);
				}
			}
			Appconst.ShowLog("线程名:" + this.OrderNumberStart + " 该线程已停止");
		}

		// Token: 0x04000025 RID: 37
		public static LogHelper log = LogFactory.GetLogger(typeof(PayClass));

		// Token: 0x04000026 RID: 38
		private Stopwatch threadStopwatch1 = new Stopwatch();

		// Token: 0x04000027 RID: 39
		private DataTable orderDt = new DataTable();

		// Token: 0x04000028 RID: 40
		public static object lockObj = new object();

		// Token: 0x04000029 RID: 41
		public int sIndex;

		// Token: 0x0400002A RID: 42
		public string OrderNumberStart;

		// Token: 0x0400002B RID: 43
		public int User_GroupId;

		// Token: 0x0400002C RID: 44
		private long 获取订单耗时 = 0L;

		// Token: 0x0400002D RID: 45
		private long 处理订单耗时 = 0L;
	}
}
